﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Logistics.Utils
{
    class CollectionUtils
    {
        public static IDictionary<String, String> FillDictionary(DataTable dt, String keyCol, String valueCol, bool addAllOption)
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            if (addAllOption)
                dict.Add("-1", "All");
            if (dt != null && dt.Rows.Count > 0
                && !String.IsNullOrEmpty(keyCol) && !String.IsNullOrEmpty(valueCol)
                && dt.Columns.Contains(keyCol) && dt.Columns.Contains(valueCol))
            {
                foreach (DataRow row in dt.Rows)
                {
                    String key = Convert.ToString(row[keyCol]);
                    String value = Convert.ToString(row[valueCol]);
                    if (!String.IsNullOrEmpty(key))
                    {
                        if (dict.ContainsKey(key))
                            dict[key] = value;
                        else
                            dict.Add(key, value);
                    }
                }
            }
            return dict;
        }
    }
}
